package com.youbug.mall.seckill.controller;

import com.youbug.common.vo.R;
import com.youbug.mall.seckill.service.ISeckillService;
import com.youbug.common.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/mall/seckill")
@Controller
public class SeckillController {

    @Autowired
    private ISeckillService seckillService;

    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody
    public R<List<SeckillSkuRedisTo>> getCurrentSeckillSkus() {

        //获取到当前可以参加秒杀商品的信息
        List<SeckillSkuRedisTo> tos = seckillService.getCurrentSeckillSkus();

        return R.ok(tos);
    }

    @GetMapping(value = "/kill")
    public String kill(@RequestParam("killId") String killId,
                                           @RequestParam("rendomCode") String rendomCode,
                                           @RequestParam("num") Integer num,
                                           Model model) {
        String orderSn = null;
        try {
            orderSn = seckillService.kill(killId, rendomCode, num);
            model.addAttribute("orderSn", orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "seckillsuccess";
    }
}
